package controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import dao.*;
import entity.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: personnel_management_system
 * @description: 主页数据返回
 * @author: Mr.Sun
 * @create: 2020-05-23 22:40
 **/

@WebServlet("/home")
public class HomeController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Map<String,Object> maps = new HashMap<>();

        try {
            //1.查询雇员表
            EmployeeDao edao = new EmployeeDao();
            DeptDao ddao = new DeptDao();
            ContractDao cdao = new ContractDao();
            JobDao jdao = new JobDao();
            RewardsDao rdao = new RewardsDao();
            TitlesDao tidao = new TitlesDao();
            TrainDao trdao = new TrainDao();

            //1.查询雇员表
            List<Employee> emps = edao.selectAll();
            //2.查询部门表
            List<Dept> dept = ddao.selectAll();
            //3.查询合同表
            List<Contract> contracts = cdao.selectAll();
            //4.查询工作变动表
            List<Job> job = jdao.selectAll();
            //5.查询奖惩表
            List<Rewards> rewards = rdao.selectAll();
            //6.查询职称表
            List<Titles> titles = tidao.selectAll();
            //7.查询培训表
            List<Train>  trains = trdao.selectAll();

            maps.put("Employee",emps);
            maps.put("Dept",dept);
            maps.put("Contract",contracts);
            maps.put("Job",job);
            maps.put("Rewards",rewards);
            maps.put("Titles",titles);
            maps.put("Train",trains);

        } catch (Exception e) {
            e.printStackTrace();
        }


        resp.setContentType("text/json;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        PrintWriter out = resp.getWriter();
        System.out.println(JSON.toJSONString(maps));

        out.println(JSONObject.toJSONString(maps));

        out.flush();
        out.close();

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
